﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kokomo.Mirror.CliMetadata.Tables;

namespace Kokomo.Mirror.CliMetadata
{
	class CliGenericTypeDefinition : CliType
	{
		public CliGenericTypeDefinition(
			TypeDefRow typeDefRow,
			CliMemberBaseInfo baseInfo,
			TypeMemberMetadata memberMetadata,
			int[] interfaceTokens,
			MetadataRowRange<GenericParamRow> genericParamRowRange
			)
			: base(
				typeDefRow,
				baseInfo,
				memberMetadata,
				interfaceTokens
			)
		{
			this._genericParamRowRange = genericParamRowRange;
		}

		private MetadataRowRange<GenericParamRow> _genericParamRowRange;
		private Type[] _genericTypeArguments;
		public override Type[] GetGenericArguments()
		{
			if (this._genericTypeArguments == null)
			{
				Type[] genericArguments = new Type[this._genericParamRowRange.RowCount];
				for (int i = 0; i < genericArguments.Length; i++)
				{
					GenericParamRow genericParamRow = this._genericParamRowRange[i];
					genericArguments[i] = new CliGenericParamType(
						genericParamRow,
						new CliMemberBaseInfo()
						{
							declaringType = this,
							reflectedType = this.ReflectedType,
							token = (((int)MetadataTableNumber.GenericParam) << 24) | (i + this._genericParamRowRange.FirstRowNumber)
						});
				}
				this._genericTypeArguments = genericArguments;
			}

			return this._genericTypeArguments;
		}

		public override bool IsGenericType
		{
			get { return true; }
		}
		public override bool IsGenericTypeDefinition
		{
			get { return true; }
		}
		public override Type MakeGenericType(params Type[] typeArguments)
		{
			return null;
			//return new CliGenericType(
			//    this._typeDefRow,
			//    this._baseInfo,
			//    new TypeMemberMetadata()
			//    {
			//        eventRows = this._eventRows,
			//        fieldRows = this._fieldRows,
			//        methodDefRows = this._methodDefRows,
			//        nestedTypeRows = this._nestedTypeRows,
			//        propertyRows = this._propertyRows
			//    },
			//    null,
			//    this,
			//    genericTypeArguments
			//    );
		}
	}
}
